字段(属性)生成器
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using Business;namespace 字段_属性_生成器{ public partial class MainForm : Form { public MainForm() { InitializeComponent(); } ServerBll sb = new ServerBll(); string ServerName = ""; string dataBaseName = ""; string tableName = ""; string inputString = ""; //存放输出字符串 bool copy = false; private void cboDataBase_MouseClick(object sender, MouseEventArgs e) { if (cboServerName.Text.Trim() == null || cboServerName.Text.Trim() == "") { MessageBox.Show("请输入一个服务器名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cboServerName.Focus(); return; } if (cboServerName.Text.Trim() != ServerName) { cboDataBase.Items.Clear(); List<string> databaseNames = sb.GetDataBaseNames(cboServerName.Text.Trim(), "master"); //for (int i = 0; i < databaseNames.Count; i ) // cboDataBase.Items.Add(databaseNames[i]); cboDataBase.DisplayMember = "name"; cboDataBase.DataSource = databaseNames; cboDataBase.Text = null; ServerName = cboServerName.Text.Trim(); } } private void cboTableName_MouseClick(object sender, MouseEventArgs e) { if (cboServerName.Text.Trim() == null || cboServerName.Text.Trim() == "") { MessageBox.Show("请输入一个服务器名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cboServerName.Focus(); return; } if (cboDataBase.Text.Trim() == null || cboDataBase.Text.Trim() == "") { MessageBox.Show("请选择一个数据库!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cboDataBase.Focus(); return; } if (cboDataBase.Text.Trim() != dataBaseName) { cboTableName.Items.Clear(); List<string> tableNames = sb.GetTables(cboServerName.Text.Trim(), cboDataBase.Text.Trim()); //for (int i = 0; i < tableNames.Count; i ) // cboTableName.Items.Add(tableNames[i]); cboTableName.DisplayMember = "name"; cboTableName.DataSource = tableNames; cboTableName.Text = null; dataBaseName = cboDataBase.Text.Trim(); } } string[] propertyName ={ "INT", "STRING", "FLOAT", "DECIMAL", "BOOL", "ENUM", "STRUCT", "CHAR", "SBYTE","SHORT","LONG","BYTE","USHORT","UINT","ULONG","DOUBLE" }; private void btnConsole_Click(object sender, EventArgs e) { #region 判断服务器名、数据库名、表名下拉框中是否有值 /* if (cboServerName.Text.Trim() == null || cboServerName.Text.Trim() == "") { MessageBox.Show("请输入一个服务器名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cboServerName.Focus(); return; } if (cboDataBase.Text.Trim() == null || cboDataBase.Text.Trim() == "") { MessageBox.Show("请选择一个数据库!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cboDataBase.Focus(); return; } if (cboTableName.Text.Trim() == null || cboTableName.Text.Trim() == "") { MessageBox.Show("请选择一个表!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cboDataBase.Focus(); return; } * */ #endregion if (txtInput.Text.Trim() == "" && cboTableName.Text.Trim() == "") { MessageBox.Show("请先输入您要操作的字段\n或选择数据库中的一个表", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } if (cboType.Text.Trim() != "") { if (cboType.Text == " 属 性") { txtOut.Text = ""; if (txtInput.Text.Trim() == "") { MessageBox.Show("您还没有输入字段呢!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } int index = txtInput.Text.Trim().IndexOf(';'); if (index < 0) { MessageBox.Show("您没有在变量后面输入(英文)分号“;”", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } string input = txtInput.Text.Trim(); string[] str = input.Split(';'); for (int i = 0; i < str.Length - 1; i ) { str[i] = str[i].Trim(); string[] s = str[i].Split(' ', '\r', '\n'); if (s.Length == 1) { MessageBox.Show("您还没有输入变量名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } string second = s[s.Length - 1].Substring(0, 1).ToLower(); //把下划线后的第一个字母变成小写 string private1 = " _" second s[s.Length - 1].Substring(1); //存放私有字段 for (int j = 0; j < propertyName.Length; j ) { if (s[0].ToUpper() == propertyName[j]) s[0] = s[0].ToLower(); } if (s[0].ToLower() == "datetime") s[0] = "DateTime"; txtOut.Text = "private " s[0] private1 ";"; //s[0]是类型 string first = (s[s.Length - 1].Substring(0, 1)).ToUpper(); s[s.Length - 1] = first (s[s.Length - 1].Substring(1)); txtOut.Text = "\r\npublic " s[0] " " s[s.Length - 1]; txtOut.Text = "\r\n{\r\n"; txtOut.Text = "\tget { return " private1 " ; }\r\n"; txtOut.Text = "\tset { " private1 " = value ; }\r\n"; txtOut.Text = "}\r\n\r\n"; } } else if (cboType.Text == " 枚 举") { if (cboTableName.Text.Trim() == "" && btnConsole.Text == "生 成 枚 举") { MessageBox.Show("不能以自定义字段来生成枚举类型!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); cboType.Focus(); return; } txtOut.Text = ""; if (txtInput.Text.Trim() == "") { MessageBox.Show("您还没有输入字段呢!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } int index = txtInput.Text.Trim().IndexOf(';'); if (index < 0) { MessageBox.Show("您没有在变量后面输入(英文)分号“;”", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } string input = txtInput.Text.Trim(); string[] str = input.Split(';'); string one = cboTableName.Text.Trim().Substring(0, 1).ToUpper(); //把表中的第一个字母变成大写 one = cboTableName.Text.Trim().Substring(1); txtOut.Text = "public enum " one "\r\n{\r\n"; for (int i = 0; i < str.Length - 1; i ) { str[i] = str[i].Trim(); string[] s = str[i].Split(' ', '\r', '\n'); string oneString = s[1].Substring(0, 1).ToUpper(); oneString = s[1].Substring(1); txtOut.Text = "\t" oneString; if (i != str.Length - 2) txtOut.Text = ",\r\n"; else txtOut.Text = "\r\n"; } txtOut.Text = "}\r\n\r\n"; } else if (cboType.Text == " 脚 本") { MessageBox.Show("此功能正在升级中……", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show("请选择一个生成类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cboType.Focus(); return; } } private void cboType_SelectedValueChanged(object sender, EventArgs e) { if (cboType.Text == " 属 性") btnConsole.Text = "生 成 属 性"; if (cboType.Text == " 枚 举") btnConsole.Text = "生 成 枚 举"; if (cboType.Text == " 脚 本") btnConsole.Text = "生 成 脚 本"; if (cboType.Text == " 脚 本") { MessageBox.Show("此功能正在升级中……", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } if (tableName != cboTableName.Text.Trim()) { LoadProperty(); } } /// <summary> /// 加载数据库中的字段到文本框中 /// </summary> private void LoadProperty() { string[] TypeString = { "varchar", "nvarchar", "ntext", "text", "nchar", "char" }; List<string> typeAndPropertys = sb.GetTypeAndProperty(cboServerName.Text.Trim(), cboDataBase.Text.Trim(), cboTableName.Text.Trim()); for (int i = 0; i < typeAndPropertys.Count; i ) { if (i % 2 == 0) { if (typeAndPropertys[i] == "bigint") typeAndPropertys[i] = "int"; for (int j = 0; j < TypeString.Length; j ) { if (typeAndPropertys[i] == TypeString[j]) //SQL中的char和nchar在C#中的数据类型是哪种 { typeAndPropertys[i] = "string"; break; } } inputString = typeAndPropertys[i] " "; } else { inputString = typeAndPropertys[i]; inputString = ";\r\n"; } } txtInput.Text = ""; txtInput.Text = inputString; tableName = cboTableName.Text.Trim(); } private void cboTableName_SelectedIndexChanged(object sender, EventArgs e) { if (cboTableName.Text.Trim() != tableName) { if (txtInput.Text.Trim() != "") { if (cboDataBase.Text.Trim() != dataBaseName) return; DialogResult dr = MessageBox.Show(" 重新选择一个表\n您要保留原有的字段吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr != DialogResult.Yes) { txtInput.Text = ""; inputString = ""; txtOut.Text = ""; LoadProperty(); btnConsole.Text = "生 成 属 性"; } else { txtOut.Text = ""; inputString = "\r\n"; LoadProperty(); btnConsole.Text = "生 成 属 性"; } cboType.Text = null; } //else //{ // LoadProperty(); //} } } private void btnCopy_Click(object sender, EventArgs e) { if (txtInput.Text.Trim() != "" && txtOut.Text.Trim() == "") { MessageBox.Show("您还没有生成属性、枚举、脚本!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } if (txtInput.Text.Trim() == "" || txtOut.Text.Trim() == "") { MessageBox.Show("您还没有输入内容呢!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } else { Clipboard.SetDataObject(txtOut.Text); //复制代码 copy = true; MessageBox.Show("代码复制成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void btnClear_Click(object sender, EventArgs e) { if (txtInput.Text.Trim() == "" && txtOut.Text.Trim() == "") { MessageBox.Show("您还没有输入内容呢!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtInput.Focus(); return; } DialogResult dr = MessageBox.Show("您确实要清空内容吗?", "清空", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr != DialogResult.No) { txtInput.Text = ""; txtOut.Text = ""; } cboType.Text = null; } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (txtOut.Text.Trim() != "" && copy == false) { DialogResult drt = MessageBox.Show("您生成的代码还没有复制\n您是否要现在复制代码?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (drt == DialogResult.Yes) { Clipboard.SetDataObject(txtOut.Text); //复制代码 copy = true; MessageBox.Show("代码复制成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); e.Cancel = true; } else { e.Cancel = false; } } } private void btnCancel_Click(object sender, EventArgs e) { if (btnCancel.Text == "取消窗体总在最前") { btnCancel.Text = "设置窗体总在最前"; this.TopMost = false; } else { btnCancel.Text = "取消窗体总在最前"; this.TopMost = true; } } private void cboDataBase_SelectedIndexChanged(object sender, EventArgs e) { cboTableName.Text = null; cboType.Text = null; txtInput.Text = ""; } string property = ""; private void cboType_SelectedIndexChanged(object sender, EventArgs e) { //if (cboTableName.Text.Trim() != "" && tableName == cboTableName.Text.Trim() && property != cboType.Text) //{ // LoadProperty(); // property = cboType.Text; //} } }}
评论